<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\common\controller\Api;
use app\common\manager\BaseManager;
use app\common\manager\CommentManager;
use app\common\manager\LogManager;
use app\common\manager\MatchManager;
use think\Exception;

/**
 * 组队接口
 */
class Match extends Api
{
    protected $noNeedLogin = ['detail','matchList','round','ranks','matchJoiner','getSignCode'];
    protected $noNeedRight = ['*'];
    protected $needRecordRight = ['apply'];


    /**
     * 赛事列表
     * @return void
     */
    public function matchList(){
        try {
            $page = $this->request->post('page') ?? 1;
            if ($page < 1){
                $page = 1;
            }

            $lat = $this->request->post('lat');
            $lng = $this->request->post('lng');
            $radius = $this->request->post('radius');
            $type = $this->request->post('type');

            $result = MatchManager::matchList($type, $lat, $lng, $radius, $page);
            if ($result['code'] == BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }


    }

    /**
     * 赛事详情
     * @return void
     */
    public function detail()
    {
        $id = $this->request->post('id');
        $long = $this->request->post('lng');
        $lat = $this->request->post('lat');
        try {
            $result = MatchManager::detail($this->user,$id,$long,$lat);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 赛事报名人员
     * @id int 组队Id
     * @page int 页码
     * @return void
     */
    public function matchJoiner()
    {
        $id = $this->request->post('id');
        $page = $this->request->post('page');
        if ($page < 1){
            $page = 1;
        }
        try {
            $result = MatchManager::matchJoiner($id,$page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 赛事评论
     * @return void
     */
    public function comment()
    {
        try {
            $comment = $this->request->post('comment');
            $image = $this->request->post('image');
            $mid = $this->request->post('mid');
            $pid = $this->request->post('pid') ?? 0;
            $data = ['content'=>$comment,'image'=>$image,'t_id'=>$mid,'up_umc_Id'=>$pid];
            $result = CommentManager::CommentAboutMatch($this->user,$data);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 删除评论
     * @return void
     */
    public function delComment()
    {
        try {
            $mid = $this->request->post('mid');
            $result = CommentManager::delCommentAboutMatch($this->user,$mid);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 赛事评论列表
     * @return void
     */
    public function commentList(){

        try {
            $page = $this->request->post('page');
            if ($page < 1){
                $page = 1;
            }
            $mid = $this->request->post('mid') ?? 1;
            $pid = $this->request->post('pid') ?? 0;
            $result = CommentManager::listAboutMatch($mid,$pid,$page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

    }

    /**
     * 门店赛事申请
     * @return void
     */
    public function apply()
    {
        $startTime = microtime(true);
        $personal = $this->request->post('personal');
        $avg_amount = $this->request->post('avg_amount');
        $holding_time = $this->request->post('holding_time') ?? 0;
        $store_id = $this->request->post('store_id');
        $holding_address = $this->request->post('holding_address') ?? '';
        $organizer = $this->request->post('organizer');
        $charge = $this->request->post('charge');
        $mobile = $this->request->post('mobile');
        try {

            $data = [
                'personal'=>$personal,
                'avg_amount'=>$avg_amount,
                'store_id'=>$store_id,
                'holding_time'=>$holding_time,
                'holding_address'=>$holding_address,
                'organizer'=>$organizer,
                'charge'=>$charge,
                'mobile'=>$mobile,
            ];
            $matchApplyValidate = new \app\api\validate\MatchApply();
            if(!$matchApplyValidate->check($data)){
                $this->error($matchApplyValidate->getError());
            }

            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['holding_time'] = strtotime($holding_time);
            $result  = MatchManager::MatchApply($this->user,$store_id,$data);
            if ($result['code'] == BaseManager::$successCode) {
                $endTime = microtime(true);
                hook('Log',[
                    'username'=>$this->user->username,
                    'user_id'=>$this->user->id,
                    'tips'=>'赛事申请',
                    'data'=>$this->request->post(),
                    'match_id'=>$result['data']['id'],
                    'start_time'=>$startTime,
                    'end_time'=>$endTime,
                    'type'=>LogManager::TYPE_CLIENT,
                    'redirect'=>LogManager::MATCH_APPLY_REDIRECT_PATH.date('Y').'/'.date('m').'/'.date('d'),
                ]);
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 赛事申请列表
     * @return void
     */
    public function applyList()
    {
        $page = $this->request->post('page') ?? 1;
        $status = $this->request->post('status');
        $storeId = $this->request->post('store_id');
        $user = $this->user;
        try {
            $result = MatchManager::applyList($user,$storeId,$status,$page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取门店最后申请记录
     * @return void
     */
    public function recorded()
    {
        $user = $this->user;
        $storeId = $this->request->post('store_id');
        try {
            $result = MatchManager::ApplyRecorded($user,$storeId);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function round()
    {
        $matchId = $this->request->post('match_id');
        try {
            $result = MatchManager::matchRoundMember($this->user,$matchId);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function start()
    {
        $startTime = microtime(true);
        $matchId = $this->request->post('match_id');
        $nextRound = $this->request->post('next');
        $nextSwitch = $nextRound =='TURN_ON' ?? false;
        try {
            $result = MatchManager::generate($this->user,$matchId,$nextSwitch);
            //print_r($result);
            if ($result['code'] == BaseManager::$successCode){
                $endTime = microtime(true);
                hook('Log',[
                    'username'=>$this->user->username,
                    'user_id'=>$this->user->id,
                    'tips'=>'赛事轮次开始',
                    'data'=>$this->request->post(),
                    'match_id'=>$matchId,
                    'start_time'=>$startTime,
                    'end_time'=>$endTime,
                    'type'=>LogManager::TYPE_CLIENT,
                    'redirect'=>LogManager::MATCH_REDIRECT_PATH.date('Y').'/'.date('m').'/'.date('d'),
                ]);
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function end()
    {
        $startTime = microtime(true);
        $matchId = $this->request->post('match_id');
        try {
            $result = MatchManager::EndMatch($this->user,$matchId);
            //print_r($result);
            if ($result['code'] == BaseManager::$successCode){
                $endTime = microtime(true);
                hook('Log',[
                    'username'=>$this->user->username,
                    'user_id'=>$this->user->id,
                    'tips'=>'赛事结束',
                    'data'=>$this->request->post(),
                    'match_id'=>$matchId,
                    'start_time'=>$startTime,
                    'end_time'=>$endTime,
                    'type'=>LogManager::TYPE_CLIENT,
                    'redirect'=>LogManager::MATCH_REDIRECT_PATH.date('Y').'/'.date('m').'/'.date('d'),
                ]);
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function score()
    {
        $startTime = microtime(true);
        $matchId = $this->request->post('match_id');
        $id = $this->request->post('id');
        $score = $this->request->post('score');
        try {
            if (is_double($score)){
                throw new Exception('请上传正确的积分');
            }
            $result = MatchManager::score($this->user,$matchId,$id,$score);
            if ($result['code'] == BaseManager::$successCode){
                $endTime = microtime(true);
                hook('Log',[
                    'username'=>$this->user->username,
                    'user_id'=>$this->user->id,
                    'tips'=>'录入成员积分',
                    'data'=>$this->request->post(),
                    'match_id'=>$matchId,
                    'start_time'=>$startTime,
                    'end_time'=>$endTime,
                    'type'=>LogManager::TYPE_CLIENT,
                    'redirect'=>LogManager::MATCH_REDIRECT_PATH.date('Y').'/'.date('m').'/'.date('d'),
                ]);
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function ranks()
    {
        $matchId = $this->request->post('match_id');
        $page = $this->request->post('page');
        try {
            $result = MatchManager::ranks($matchId,$page);
            if ($result['code'] == BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function getSignCode()
    {
        $matchId = $this->request->post('match_id');
        try {
            $result = MatchManager::getSignCode($matchId);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

//    public function signRound()
//    {
//        $verifyCode = $this->request->post('verify_code');
//        try {
//            $result = MatchManager::signRound($this->user,$verifyCode);
//            if ($result['code']==BaseManager::$successCode){
//                $this->success($result['message'],$result['data']);
//            }else{
//                throw new Exception($result['message']);
//            }
//        }catch (Exception $exception){
//            $this->error($exception->getMessage());
//        }
//    }

    public function signRound()
    {
        $lat = $this->request->post('lat');
        $lng = $this->request->post('lng');
        $match_id = $this->request->post('match_id');
        try {
            $result = MatchManager::signRound($this->user,$match_id,$lat,$lng);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function protocolSign()
    {
        $url = $this->request->post('url');
        $match_id = $this->request->post('match_id');
        try {
            $result = MatchManager::protocolSign($this->user,$match_id,$url);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }


}
